<template>
  <div class="custom-table-list-wrapper">
    <div class="search" :class="{ active: toggleSearchStatus }">
      <div class="search-left" ref="searchFromRef">
        <el-form class="search-form" inline>
          <el-form-item label="访客状态">
            <el-select
              class="search-select"
              v-model="searchParams.isBlack"
              placeholder="请选择"
            >
              <el-option
                v-for="(item, index) of dict['balckStatus']"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="关键词">
            <el-input
              class="search-input"
              v-model="searchParams.keywords"
              placeholder="请输入访客姓名"
              :suffix-icon="Search"
            ></el-input>
          </el-form-item>
        </el-form>
        <a
          class="search-left-toggle"
          v-if="showToggle"
          @click="handleToggleSearch"
        >
          <svg-icon :name="toggleSearchStatus ? 'up' : 'down'" />
          {{ toggleSearchStatus ? '收起' : '展开' }}
        </a>
      </div>
      <div class="search-right">
        <el-button type="primary" @click="initTableList(1)"
          ><svg-icon name="search" size="14px" />搜索</el-button
        >
        <el-button @click="handleReset()"
          ><svg-icon name="reset" size="17px" />重置</el-button
        >
      </div>
    </div>
    <div class="content">
      <div class="content-table">
        <el-table class="custom-table" :data="tableData">
          <el-table-column type="index" label="序号" width="60px" />
          <el-table-column prop="name" label="访客姓名" />
          <el-table-column prop="phone" label="联系方式">
            <template #default="scope">
              {{
                scope.row.phone.replace(/^(\d{3})(\d{4})(\d{4})$/, '$1****$3')
              }}
            </template>
          </el-table-column>
          <el-table-column prop="idCard" label="身份证号">
            <template #default="scope">
              {{
                scope.row.idCard.replace(
                  /(\w{2})\w*(\w{2})/,
                  '$1**************$2',
                )
              }}
            </template>
          </el-table-column>
          <el-table-column prop="visitCount" label="访问次数" />
          <el-table-column prop="visitTime" label="最近一次访问时间" />
          <el-table-column prop="status" label="访客状态">
            <template #default="scope">
              <el-tag :type="getDictStyle('balckStatus', scope.row.isBlack)">{{
                getDictValue('balckStatus', scope.row.isBlack)
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="160px">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="
                  handleDetail(
                    { name: scope.row.name, idCard: scope.row.idCard },
                    'visitorListDetail',
                    true,
                  )
                "
                >详情</el-button
              >
              <el-button
                link
                type="primary"
                @click="handleChangeStatus(scope.row)"
                v-if="scope.row.isBlack === 0"
                >加入黑名单</el-button
              >
              <el-button
                link
                type="primary"
                @click="handleChangeStatus(scope.row)"
                v-else
                >移除黑名单</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="content-page">
        <el-pagination
          class="custom-pagination"
          :current-page="paginationParams.pageIndex"
          :page-size="paginationParams.pageSize"
          background
          :page-sizes="[10, 20, 30, 50]"
          layout="total, prev, pager, next, sizes, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="initTableList"
        />
      </div>
    </div>
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import { useTableList } from '@/hooks/useTableList'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  getBlackList,
  blackUpdate,
} from '@/api/manager/visitorManage/visitorList.js'
import { useDict } from '@/hooks/useDict'
const { dict, getDictValue, getDictStyle } = useDict(['balckStatus'])
const searchParams = ref({})
const {
  Search,
  toggleSearchStatus,
  showToggle,
  handleToggleSearch,
  tableData,
  paginationParams,
  total,
  initTableList,
  handleSizeChange,
  handleReset,
  handleDelete,
  addRef,
  detailRef,
  currentDetail,
  handleAdd,
  handleEdit,
  handleDetail,
  handleClose,
  searchFromRef,
} = useTableList(searchParams, getBlackList)

const handleChangeStatus = async (val) => {
  ElMessageBox.confirm(`是否切换访客状态`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      const res = await blackUpdate({
        idCard: val.idCard,
        isBlack: val.isBlack === 1 ? 0 : 1,
      })
      if (res.success) {
        ElMessage.success('切换成功')
        val.isBlack = val.isBlack === 1 ? 0 : 1
      } else {
        ElMessage.error(res.message)
      }
    })
    .catch((error) => {
      console.log(error)
    })
}

onMounted(() => {
  initTableList(1)
})
</script>
<style lang="scss" scoped>
@import '@/assets/styles/table.scss';
.content-table {
  height: calc(100% - 50px);
}
</style>
